
/**
 *  Class: Datastructures, CSI 326 Fall 2011
 *  Members: Christopher Nerf, Greg Shevach, Mesfin Solomon, Vidat Muslim
 *  Project: Project 1
 *  Scope: Implement a stack using one of the basic containers we have or will discuss. Example: A vector or list based stack.
 *  	   Implement a queue using one of the basic containers we have or will discuss. Example: A circular array or list based
 *		   queue
 *		   Develop and implement a palindrome checker that uses your stack and/or queue.
 *				 The data to be checked as a palindrome will consist of string characters, possibly containing whitespace and
 *				punctuation.
 *				 A palindrome reads the same forward as backward, ignoring case, whitespace and punctuation.
 *				 The strings to check will be stored in a file, one string per line. The file will contain the string quit to signify
 *				the end of file.
 *				 The strings can be of any length
 *  Version: 0.0 - Initial Pass
 */

/**
*
*  This file contains the Palindrome Checker using a stack for backwards checking and a queue for forwards checking
*  compare the values popped from the stack and deqeued, if they are not equal it is not a palindrome
*  quit is a reserved word to signify the end of file
*
*/


public class PalindromeChecker 
{

	private Stack s;
	private Queue q;
	private String filename ="";
	private MyFileReader mfr;
	
	public PalindromeChecker(String filename)
	{
		filename = "C:\\Users\\nerfc\\Desktop\\SVN-Data Structures\\Project 1\\DataStructuresProject1\\src\\palindrome.txt"
		//create new, empty stacks and queues
		//create new MyFileReader with filename path.
		mfr = new MyFileReader(filename);
		s = new Stack();
		q = new Queue();
	}
	
	public boolean weakCompareNext() throws java.io.EOFException
	{
		//Reinitialize s and q to new stacks and queues
		//Weird memory issue where the stack and queue would leave junk in the object and cause everything to screw up
		//This is the easiest fix.
		//TODO: Find a better way to fix this rather than reinitialize object, go to the stack and queue pop functions
		//		Something might be borked there on a deeper level.
		s = new Stack();
		q = new Queue();
		boolean isPalindromic = true;
		char sVal, qVal;
		//sVal = qVal = null;
		//rawPalindrome contains spaces and punctuation and capitalized
		String rawPalindrome = new String(mfr.getNext());
		//remove capitalization
		rawPalindrome = rawPalindrome.toUpperCase();
		//remove space
		rawPalindrome = rawPalindrome.replace(" ", "");
		//remove punctuation
		
		//TODO: Y U NO THROW EXCEPTION!?
		//if(rawPalindrome == "QUIT")
		//{
		//	throw new java.io.EOFException("End of file reached");
		//}
		
		//convert rawPalindrome into character array to be added to queues and pushed to stacks
		char[] palindrome = rawPalindrome.toCharArray();
		
		//Added in by Greg, properly reads "QUIT" and thro
		if(palindrome[0] == 'Q' && palindrome[1] == 'U' && palindrome[2] == 'I' && palindrome[3] == 'T') {
			throw new java.io.EOFException("End of file reached");
		}
		

		for(int i = 0; i<palindrome.length; ++i)
		{
			System.out.print(palindrome[i]);
			s.push(palindrome[i]);
			q.add(palindrome[i]);
		}
		//while the queue and stack are not empty, keeping poppin away
		//if the pop does not equal the dequeue, return false
		//return true outside loop

		
		//format tab
		System.out.print("\t");
		
		while(true)
		{
				
			if(s.isEmpty() || q.isEmpty())
				break;
			else
			{
				sVal = s.pop().toString().charAt(0);
				qVal = q.removeFront().toString().charAt(0);
			}
			
			if(sVal != qVal)
			{
				isPalindromic = false;
				break;
			}
			
			
		}
		return isPalindromic;
	}
	
	
}
